<script lang="ts" setup>
import Tabbar from './Tabbar.vue'
import { uniEvent } from '@/enums'
import AgreementPolicyDialog from '@/components/AgreementPolicyDialog.vue'

withDefaults(defineProps<{
  scrollY?: boolean
  tabbar?: boolean
  bg?: string
}>(), {
  scrollY: true,
  tabbar: true,
  bg: 'transparent',
})

const emits = defineEmits<{
  (e: 'init'): void
}>()

const AgreementPolicyDialogRef = ref<typeof AgreementPolicyDialog>()

uni.$on(uniEvent.隐私协议弹窗, () => {
  isAgreement.value = true
  AgreementPolicyDialogRef.value?.close()
})

const isAgreement = ref(false)

onMounted(() => {
  try {
    // @ts-expect-error
    wx.getPrivacySetting({
      success: async (res: any) => {
        console.log(res) // 返回结果为: res = { needAuthorization: true/false, privacyContractName: '《xxx隐私保护指引》' }
        if (res.needAuthorization) {
          isAgreement.value = false
          // 需要弹出隐私协议
          console.log(AgreementPolicyDialogRef.value)
          AgreementPolicyDialogRef.value?.show()
        }
        else {
          uni.$emit(uniEvent.隐私协议弹窗)
          emits('init')
        }
      },
      fail: () => { },
    })
  }
  catch (e) {
    // TODO handle the exception
    uni.$emit(uniEvent.隐私协议弹窗)
    emits('init')
  }
})

const handleAgreement = async () => {
  isAgreement.value = true
  emits('init')
  uni.$emit(uniEvent.隐私协议弹窗)
}
</script>

<template>
  <scroll-view :scroll-y="scrollY" class="tabBarPageHeight" :style="{ backgroundColor: bg }">
    <slot />
  </scroll-view>
  <!-- <Tabbar  :z="tabbar && isAgreement ? 999 : 99" /> -->
  <AgreementPolicyDialog ref="AgreementPolicyDialogRef" @confirm="handleAgreement" />
</template>

<style lang="scss" scoped>
.tabBarPageHeight_tabbar {
  height: calc(100vh - 44px - 8px - env(safe-area-inset-bottom));
}

.tabBarPageHeight {
  height: 100vh;
}
</style>
